import { types } from "mobx-state-tree"
import { Toast } from "antd-mobile"
import http from "../services"

export const LoginStoreModel = types
  .model("HomeStoreModel", {
    // 登录模块
    username: "",
    password: "",
    usernameOpenClear: false,
    passwordOpenClear: false,
    isPasswordOpen: false,
    rememberPswCheck: false,
    mobileCode: "+86",
    mobile: "",
    verification: "",
    mobileOpenClear: false,
    verificationOpenClear: false,
    showCaptcha: false,
    tabPage: 0,

    // 忘记密码模块
    forgetStep: 0,
    forgetUsername: "",
    forgetUsernameOpenClear: false,
    showErrorModal: false,
    userMobile: "",
    confirmPassword: "",
    confirmPasswordOpenClear: false,
    isConfirmPasswordOpen: false,

    // 注册模块
    registerStep: 1
  })

  .actions((self: any) => ({
    // 登录模块
    usernameOnChange(value: string) {
      self.username = value
      if (self.username.length > 0) {
        self.usernameOpenClear = true
      } else {
        self.usernameOpenClear = false
      }
    },

    passwordOnChange(value: string) {
      self.password = value
      if (self.password.length > 0) {
        self.passwordOpenClear = true
      } else {
        self.passwordOpenClear = false
      }
    },

    usernameClear() {
      self.username = ""
      self.usernameOpenClear = false
    },

    passwordClear() {
      self.password = ""
      self.passwordOpenClear = false
    },

    changePasswordStyle() {
      self.isPasswordOpen = !self.isPasswordOpen
    },

    rememberPswChange() {
      self.rememberPswCheck = !self.rememberPswCheck
    },

    mobileOnChange(value: string) {
      self.mobile = value
      if (self.mobile.length > 0) {
        self.mobileOpenClear = true
      } else {
        self.mobileOpenClear = false
      }
    },

    mobileClear() {
      self.mobile = ""
      self.mobileOpenClear = false
    },

    verificationClear() {
      self.verification = ""
      self.verificationOpenClear = false
    },

    verificationOnChange(value: string) {
      self.verification = value
      if (self.verification.length > 0) {
        self.verificationOpenClear = true
      } else {
        self.verificationOpenClear = false
      }
    },

    captchaShow() {
      self.showCaptcha = true
    },

    captchaClose() {
      self.showCaptcha = false
    },

    loginCallback(err: any, data: any) {
      if (err === null && data.validate !== null) {
        const param = {
          mobile: self.mobile,
          telArea: 86,
          validate: data.validate
        }
        http.post("/api/gl/login/code", param).then(data => {
          console.log(data)
        })
      } else {
        Toast.info("验证码错误")
      }
    },

    tabsOnChange(tab: any, index: number) {
      self.tabPage = index
    },

    login() {
      if (self.tabPage === 0) {
        const param = {
          username: self.username,
          password: self.password,
          productType: 9,
          code: "",
          validate: "",
          uuid: 1808769079
        }
        http.post("/api/gl/login/username", param).then((data: any) => {
          if (data.code === 1) {
            http.setAuth(
              data.data.token,
              data.data.id,
              data.data.username,
              self.rememberPswCheck,
              self.password
            )
            self.loginJump()
          } else {
            Toast.info(data.message)
          }
        })
      } else {
        const param = {
          mobile: self.mobile,
          code: self.verification,
          telArea: 86,
          productType: 9
        }
        http.post("/api/gl/login/mobile", param).then((data: any) => {
          if (data.code === 1) {
            http.setAuth(
              data.data.token,
              data.data.id,
              data.data.username,
              self.rememberPswCheck,
              self.password
            )
            self.loginJump()
          } else {
            Toast.info(data.message)
          }
        })
      }
    },

    loginJump() {
      const param = {
        gameId: 3,
        type: 0
      }
      http.post("/api/game/jump", param).then((data: any) => {
        http.setXJProxy()
        const url = data.data
        const newUrl = url.replace("http://xj-sb-asia-mrball.prdasbbwla1.com/", "")
        http.getXJ(newUrl).then((data: any) => {
          self.getXJParam()
        })
      })
    },

    getXJParam() {
      const param = {
        pid: 0,
        v: 0
      }

      http.postXJParam("/zh-cn/serv/getmenu", param).then(data => {
        if (data) {
        }
      })
    },

    // 忘记密码模块
    forgetUsernameOnChange(value: string) {
      self.forgetUsername = value
      if (self.forgetUsername.length > 0) {
        self.forgetUsernameOpenClear = true
      } else {
        self.forgetUsernameOpenClear = false
      }
    },

    forgetUsernameClear() {
      self.forgetUsername = ""
      self.forgetUsernameOpenClear = false
    },

    forgetStepOnChange() {
      switch (self.forgetStep) {
        case 0:
          self.captchaShow()
          break;
        case 1:
          self.sendForgetCode()
          break;
        default:
      }
    },

    forgetPasswordCallback(err: any, data: any) {
      if (err === null && data.validate !== null) {
        const param = {
          username: self.forgetUsername,
          code: "",
          uuid: "",
          validate: data.validate
        }
        http.post("/api/gl/forget/info", param).then((data: any) => {
          if (data.code === 1) {
            if (data.data.telephone === "") {
              setTimeout(() => {
                self.openShowErrorModal()
              }, 1000)
            } else {
              self.setForgetMobile(data.data.telephone)
              self.setForgetNextStep()
            }
          }
        })
      }
    },

    setForgetMobile(mobile: string) {
      self.userMobile = mobile
    },

    openShowErrorModal() {
      self.showErrorModal = true
    },

    closeErrorModal() {
      self.showErrorModal = false
    },

    setForgetNextStep() {
      self.forgetStep = self.forgetStep + 1
    },

    sendForgetCode() {
      const param = {
        username: self.forgetUsername,
        type: "mobile",
        uuid: "",
        imgCode: ""
      }
      http.post("/api/gl/forget/code", param).then((data: any) => {
        if (data.code === 1 && self.forgetStep !== 2) {
          self.setForgetNextStep()
        }
      })
    },

    confirmPasswordClear() {
      self.confirmPassword = ""
      self.confirmPasswordOpenClear = false
    },

    confirmPasswordOnChange(value: string) {
      self.confirmPassword = value
      if (self.confirmPassword.length > 0) {
        self.confirmPasswordClear = true
      } else {
        self.confirmPasswordClear = false
      }
    },

    changeConfirmPasswordStyle() {
      self.isConfirmPasswordOpen = !self.isConfirmPasswordOpen
    },

    resetPassword(history: any) {
      const param = {
        username: self.forgetUsername,
        type: "mobile",
        code: self.verification,
        newPwd: self.password,
        uuid: "",
        imgCode: ""
      }
      http.post("/api/gl/forget/reset/password", param).then((data: any) => {
        if (data.code === 1) {
          history.push("/login")
        }
      })
    },

    // 注册相关
    registerCallback(err: any, data: any) {
      if (err === null && data.validate !== null) {
        const param = {
          username: self.username,
          password: self.password,
          validate: data.validate,
          uuid: ""
        }

        http.post("/api/gl/register/username", param).then((data: any) => {
          if (data.code === 1) {
            console.log(data)
          }
        })
      }
    }
  }))
